package edu.westga.sketchit2009cs.gameEngine;

import java.io.*;

/**
 * Reads a specified file, imports it and converts it
 * into a string of words, with the first word being
 * the category.
 * 
 * @author RJ Hill
 *
 */
public class WordFileReader {

	private File theFile;
	private BufferedReader input;
	
	public WordFileReader() {
		
	}
	
	/**
	 * Reads data from a file, imports it and makes it into a string.
	 * <p>
	 * Requires: theFile != null
	 * <p>
	 * Ensures: contents != null
	 * @param fileName the name of the file with extension and path
	 * @return
	 */
	public String readFromFile(String fileName) {
		this.theFile = new File(fileName);
		
		//read each line into a string
		StringBuilder contents = new StringBuilder();
	    
	    try {
	    	
	    	//use buffering, reading one line at a time
	    	//FileReader always assumes default encoding is OK!
	    	input =  new BufferedReader(new FileReader(this.theFile));
	    	try {
	    		String line = null; //not declared within while loop

	    		//returns the content of a line MINUS the newline.
	    		while (( line = input.readLine()) != null) {
	    			contents.append(line);
	    			contents.append(System.getProperty("line.separator"));
	    		}
	    	}
	    	finally {
	    		input.close();
	    	}
	    }
	    catch (IOException ex){
	    	ex.printStackTrace();
	    }
	    return contents.toString(); 
	}
}
